package com.example.myapplication

import com.example.myapplication.my_view_model.MyViewModel
import android.app.appsearch.observer.ObserverCallback
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get


//MARK: - 通过LievData监听值改变
class ConstantsActivity : AppCompatActivity() {
    lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_constants)
        var myViewModel = ViewModelProvider(this)[MyViewModel::class.java]

        val btn = findViewById<Button>(R.id.button4)
        val btn2 = findViewById<Button>(R.id.button5)
        textView = findViewById<TextView>(R.id.textView2)

        myViewModel.numLiveData.observe(this,object: Observer<Int>{

            override fun onChanged(value: Int) {
                textView.text = value.toString()
            }
        })

        btn.setOnClickListener(object : OnClickListener{
            override fun onClick(v: View?) {
                myViewModel.setNumLiveData(1)
            }
        })

        btn2.setOnClickListener(object: OnClickListener{
            override fun onClick(v: View?) {
                myViewModel.setNumLiveData(-1)
            }
        })
    }
}
//MARK: - 通过ViewModel保存状态
//class ConstantsActivity : AppCompatActivity() {
//    lateinit var textView: TextView
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_constants)
//        var myViewModel = ViewModelProvider(this)[MyViewModel::class.java]
//
//        val btn = findViewById<Button>(R.id.button4)
//        textView = findViewById<TextView>(R.id.textView2)
//        textView.text = myViewModel.num.toString()
//
//        btn.setOnClickListener(object : OnClickListener{
//            override fun onClick(v: View?) {
//                myViewModel.num += 1
//                textView.text = myViewModel.num.toString()
//            }
//        })
//    }
//}


// MARK: - 通过onSaveInstanceState 保存状态

//class ConstantsActivity : AppCompatActivity() {
//    lateinit var textView: TextView
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_constants)
//
//        val btn = findViewById<Button>(R.id.button4)
//        textView = findViewById<TextView>(R.id.textView2)
//        if (savedInstanceState != null){
//           val str = savedInstanceState.getString("Key")
//            if (str != null){
//                textView.text = str
//            }
//        }
//
//        btn.setOnClickListener(object : OnClickListener{
//            override fun onClick(v: View?) {
//                textView.text = "test"
//            }
//        })
//    }
//
//    override fun onSaveInstanceState(outState: Bundle) {
//        super.onSaveInstanceState(outState)
//        val t =  textView.text.toString()
//
//        outState.putString("Key",t)
//    }
//
//
//}